﻿using System;
using System.Data;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class CustomerCare : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        string emailTo = string.Empty;
        string content = string.Empty;
        string subject = string.Empty;
        //string msgConent = string.Empty;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetCustomerCare();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnSendMail.Disable(true);
            }
        }
        //Store
        protected void StoreCustomerCare_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            txtDateBeginFillter.Text = string.Empty;
            txtDateEndFillter.Text = string.Empty;
            GetCustomerCare();
        }
        protected void StoreCustomerCare_Detail_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            if (_flag)
            {
                GetCustomerCareTemp();
            }
            else
            {
                GetCustomerCareEdit(int.Parse(txtIDCustomerCare.Text));
            }
        }
        protected void StoreCustomerCareAdd_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetCustomer();
            GetCustomerType();
            SelectBoxTypeCustomer.Text = string.Empty;
            txtSearchChild.Text = string.Empty;
        }
        protected void StoreForm_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetForm();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            ClearText();
            WindowsOrderCustomerCare.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerCare.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    GetCustomerCareEdit(int.Parse(row.RecordID));
                    WindowsOrderCustomerCare.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var sm = grForm.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        var obj = new EtCustomerCare { ID = int.Parse(row.RecordID) };
                        BllCustomerCare.Delete(obj);
                    }
                GetForm();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnFillter_Click(object sender, DirectEventArgs e)
        {
            GetOrderFillterDate();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnAddCustomer_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerCareAdd.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            GetCustomer();
            GetCustomerType();
            WindowCustomerCare.Show();
        }
        protected void btnDelMaterial_Click(object sender, DirectEventArgs e)
        {
            try
            {
                if (_flag)
                {
                    var sm = grCustomerCare_Detail.GetSelectionModel() as CheckboxSelectionModel;
                    if (sm != null)
                        foreach (var row in sm.SelectedRows)
                        {
                            var obj = new EtCustomerCareDetail { ID_Detail = int.Parse(row.RecordID) };
                            BllCustomerCareDetail.Delete(obj);
                        }
                    GetCustomerCareTemp();
                }
                else
                {
                    var sm = grCustomerCare_Detail.GetSelectionModel() as CheckboxSelectionModel;
                    if (sm != null)
                        foreach (var row in sm.SelectedRows)
                        {
                            var obj = new EtCustomerCareDetail { ID_Detail = int.Parse(row.RecordID) };
                            BllCustomerCareDetail.Delete(obj);
                        }
                    GetCustomerCareEdit(int.Parse(txtIDCustomerCare.Text));
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsOrderCustomerCare.Close();
        }
        protected void btnRefreshCustomerCareCode_Click(object sender, DirectEventArgs e)
        {
            txtCustomerCareCode.Text = AutoCode();
        }
        protected void btnOrderCode_Click(object sender, DirectEventArgs e)
        {
            var sm = grForm.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                GetForm();
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowViewForm.Show();
        }
        protected void btnReloadText_Click(object sender, DirectEventArgs e)
        {

        }
        protected void btnSelectCustomerCare_Click(object sender, DirectEventArgs e)
        {
            if (_flag)
            {
                InsertCustomerCareDetail();
            }
            else
            {
                UpdateCustomerCareDetail();
            }
        }
        protected void btnSendMail_Click(object sender, DirectEventArgs e)
        {
            var _clsGetCustomerCare = new BllCustomerCare();
            var _clsGetSendMail = new BllCustomerCareDetail();
            var _clsGetMail = new BllMail();
            var _dtGetMail = _clsGetMail.GetMail(string.Empty);
            var _dtGetSendMail = _clsGetSendMail.GetCustomerCareDetailDetailEdit(int.Parse(txtIDCustomerCare.Text), 0);
            var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCareEdit(int.Parse(txtIDCustomerCare.Text));
            if (_dtGetSendMail != null && _dtGetSendMail.Rows.Count > 0)
            {
                if (_dtGetMail != null && _dtGetMail.Rows.Count > 0)
                {
                    /*
                    * username  :  tài khoản của Gmail
                    * password  :  mật khẩu của Gmail
                    * port      :  Port của Gmail (mặc định là 587)
                    * host      :  Host của Gmail (mặc định là smtp.gmail.com)
                    * emailFrom :  Mail của người gửi
                    * titileFrom:  Tiêu đề Mail
                    * emailTo   :  Mail của người nhận hoặc List Mail
                    * content   :  Nội dung Mail cho người nhận Mail
                    * subject   :  Chủ đề Mail cho người nhận
                    * msgConent :  Show thông báo khi gửi Mail thành công hoặc thất bại
                    */
                    var username = _dtGetMail.Rows[0]["Email"].ToString();
                    var password = _dtGetMail.Rows[0]["Passwords"].ToString();
                    var port = int.Parse(_dtGetMail.Rows[0]["Port"].ToString());
                    var host = _dtGetMail.Rows[0]["Host"].ToString();
                    var emailFrom = _dtGetMail.Rows[0]["Email"].ToString();
                    var titileFrom = _dtGetMail.Rows[0]["Subjects"].ToString();
                    if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
                    {
                        subject += _dtGetCustomerCare.Rows[0]["Subjects"].ToString();
                        content += _dtGetCustomerCare.Rows[0]["Content"] +
                                  Environment.NewLine;
                        content += "<b>Biểu mẫu đi kèm</b>" + Environment.NewLine;
                        content += _dtGetCustomerCare.Rows[0]["Note"].ToString();
                        for (int j = 0; j < _dtGetSendMail.Rows.Count; j++)
                        {
                            if (j != _dtGetSendMail.Rows.Count - 1)
                            {
                                emailTo += _dtGetSendMail.Rows[j]["Contact_Email"] + ",";
                            }
                            else
                            {
                                emailTo += _dtGetSendMail.Rows[j]["Contact_Email"];
                            }
                        }
                        txtEmailTest.Text += emailTo;
                        LibGlobal.SendMail.SendMailEvent(username, password, port, host, emailFrom,
                                   titileFrom,
                                    txtEmailTest.Text, content, subject, string.Empty);
                    }
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Gửi Mail thành công !");
                    txtEmailTest.Text = string.Empty;
                }
            }
        }
        //CellClick
        protected void CellForm_Click(object sender, DirectEventArgs e)
        {
            var sm = grForm.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                GetFormEdit(int.Parse(id));
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowViewForm.Close();
        }
        //Double_Click
        protected void CustomerCareDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            ClearText();
            int ID_CustomerCare = int.Parse(e.ExtraParams["ID"]);
            GetCustomerCareEdit(ID_CustomerCare);
            WindowsOrderCustomerCare.Show();
        }
        //Editor
        protected void UpdateMaterials_Click(object sender, DirectEventArgs e)
        {
            var grCustomerCare_DetailEdit = e.ExtraParams["grCustomerCare_Detail"];
            var dtGetCustomerDetail = JSON.Deserialize<DataTable>(grCustomerCare_DetailEdit);
            if (_flag)
            {
                foreach (DataRow rows in dtGetCustomerDetail.Rows)
                {
                    var obj = new EtCustomerCareDetail
                    {
                        ID_Detail = int.Parse(rows["ID_Detail"].ToString()),
                        Subjects = rows["Subjects"].ToString(),
                        Content = rows["Content"].ToString()
                    };
                    BllCustomerCareDetail.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                }
                GetCustomerCareTemp();
            }
            else
            {
                foreach (DataRow rows in dtGetCustomerDetail.Rows)
                {
                    var obj = new EtCustomerCareDetail
                    {
                        ID_Detail = int.Parse(rows["ID_Detail"].ToString()),
                        Subjects = rows["Subjects"].ToString(),
                        Content = rows["Content"].ToString()
                    };
                    BllCustomerCareDetail.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                }
                GetCustomerCareEdit(int.Parse(txtIDCustomerCare.Text));
            }
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsGetCustomerCare = new BllCustomerCare();
            var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCare(txtSearch.Text);
            if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
            {
                StoreCustomerCare.DataSource = _dtGetCustomerCare;
                StoreCustomerCare.DataBind();
            }
            else
            {
                grCustomerCare.GetStore().RemoveAll();
            }
        }
        protected void SearchChild_Event(object sender, DirectEventArgs e)
        {
            var _clsGetCustomer = new Customer_BLL();
            var _dtGetCustomer = _clsGetCustomer.BindGirdtb_Customer(txtSearchChild.Text);
            if (_dtGetCustomer != null && _dtGetCustomer.Rows.Count > 0)
            {
                StoreCustomerCareAdd.DataSource = _dtGetCustomer;
                StoreCustomerCareAdd.DataBind();
            }
            else
            {
                grCustomerCareAdd.GetStore().RemoveAll();
            }
        }
        //Select-box
        protected void SelectBoxTypeCustomer_Change(object sender, DirectEventArgs e)
        {
            var _clsGetCustomer = new Customer_BLL();
            if (String.IsNullOrEmpty(SelectBoxTypeCustomer.Text))
            {
                var _dtGetCustomer = _clsGetCustomer.BindGirdtb_Customer(string.Empty);
                if (_dtGetCustomer != null && _dtGetCustomer.Rows.Count > 0)
                {
                    StoreCustomerCareAdd.DataSource = _dtGetCustomer;
                    StoreCustomerCareAdd.DataBind();
                }
                else
                {
                    grCustomerCareAdd.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetCustomer =
                    _clsGetCustomer.GetCustomerFillter(int.Parse(SelectBoxTypeCustomer.SelectedItem.Value));
                if (_dtGetCustomer != null && _dtGetCustomer.Rows.Count > 0)
                {
                    StoreCustomerCareAdd.DataSource = _dtGetCustomer;
                    StoreCustomerCareAdd.DataBind();
                }
                else
                {
                    grCustomerCareAdd.GetStore().RemoveAll();
                }
            }
        }
        #endregion

        #region[Method]
        private void GetCustomerCare()
        {
            var _clsGetCustomerCare = new BllCustomerCare();
            var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCare(txtSearch.Text);
            if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
            {
                StoreCustomerCare.DataSource = _dtGetCustomerCare;
                StoreCustomerCare.DataBind();
            }
            else
            {
                grCustomerCare.GetStore().RemoveAll();
            }
        }
        private void GetCustomerCareTemp()
        {
            var _clsGetCustomerCare = new BllCustomerCareDetail();
            var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCareDetailDetailTemp(1);
            if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
            {
                StoreCustomerCare_Detail.DataSource = _dtGetCustomerCare;
                StoreCustomerCare_Detail.DataBind();
            }
            else
            {
                grCustomerCare_Detail.GetStore().RemoveAll();
            }
        }
        private void GetCustomerCareEdit(int id)
        {
            var _clsGetCustomerCare = new BllCustomerCare();
            var _clsGetCustomerCareDetail = new BllCustomerCareDetail();
            var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCareEdit(id);
            var _dtGetCustomerCareDetail = _clsGetCustomerCareDetail.GetCustomerCareDetailDetailEdit(id, 0);
            if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
            {
                txtIDCustomerCare.Text = _dtGetCustomerCare.Rows[0]["ID"].ToString();
                txtIDForm.Text = _dtGetCustomerCare.Rows[0]["ID_Form"].ToString();
                txtCustomerCareCode.Text = _dtGetCustomerCare.Rows[0]["Code"].ToString();
                txtFormName.Text = _dtGetCustomerCare.Rows[0]["Form_Name"].ToString();
                txtDateBegin.Text = _dtGetCustomerCare.Rows[0]["DateBegin"].ToString();
                txtDateEnd.Text = _dtGetCustomerCare.Rows[0]["DateEnd"].ToString();
                txtTitileSubject.Text = _dtGetCustomerCare.Rows[0]["Subjects"].ToString();
                txtNote.Text = _dtGetCustomerCare.Rows[0]["Content"].ToString();
                if ((bool)_dtGetCustomerCare.Rows[0]["IsActive"])
                {
                    chkCheck.Checked = true;
                }
                else
                {
                    chkCheck.Checked = false;
                }
                if (_dtGetCustomerCareDetail != null && _dtGetCustomerCareDetail.Rows.Count > 0)
                {
                    StoreCustomerCare_Detail.DataSource = _dtGetCustomerCareDetail;
                    StoreCustomerCare_Detail.DataBind();
                }
                else
                {
                    grCustomerCare_Detail.GetStore().RemoveAll();
                }
            }
        }
        private void GetCustomer()
        {
            var _clsGetCustomer = new Customer_BLL();
            var _dtGetCustomer = _clsGetCustomer.BindGirdtb_Customer(string.Empty);
            if (_dtGetCustomer != null && _dtGetCustomer.Rows.Count > 0)
            {
                StoreCustomerCareAdd.DataSource = _dtGetCustomer;
                StoreCustomerCareAdd.DataBind();
            }
            else
            {
                grCustomerCareAdd.GetStore().RemoveAll();
            }
        }
        private void GetCustomerType()
        {
            var _clsGetCustomerType = new Customer_Type_BLL();
            var _dtGetCustomerType = _clsGetCustomerType.BindGirdtb_Customer_Type(string.Empty);
            if (_dtGetCustomerType != null && _dtGetCustomerType.Rows.Count > 0)
            {
                StoreTypeCustomer.DataSource = _dtGetCustomerType;
                StoreTypeCustomer.DataBind();
            }
            else
            {
                SelectBoxTypeCustomer.GetStore().RemoveAll();
            }
        }
        private void GetForm()
        {
            var _clsForm = new BllForm();
            var _dtForm = _clsForm.GetForm(string.Empty);
            if (_dtForm != null && _dtForm.Rows.Count > 0)
            {
                StoreForm.DataSource = _dtForm;
                StoreForm.DataBind();
            }
            else
            {
                grForm.GetStore().RemoveAll();
            }
        }
        private void GetFormEdit(int id)
        {
            var _clsGetFormEdit = new BllForm();
            var _dtGetFormEdit = _clsGetFormEdit.GetFormEdit(id);
            if (_dtGetFormEdit != null && _dtGetFormEdit.Rows.Count > 0)
            {
                txtIDForm.Text = _dtGetFormEdit.Rows[0]["ID_Form"].ToString();
                txtFormName.Text = _dtGetFormEdit.Rows[0]["Form_Name"].ToString();
            }
        }
        private void GetOrderFillterDate()
        {
            var _clsGetCustomerCare = new BllCustomerCare();
            if (String.IsNullOrEmpty(txtDateBeginFillter.Text) && String.IsNullOrEmpty(txtDateEndFillter.Text))
            {
                var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerCare(txtSearch.Text);
                if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
                {
                    StoreCustomerCare.DataSource = _dtGetCustomerCare;
                    StoreCustomerCare.DataBind();
                }
                else
                {
                    grCustomerCare.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetCustomerCare = _clsGetCustomerCare.GetCustomerFillter(txtDateBeginFillter.Text, txtDateEndFillter.Text);
                if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
                {
                    StoreCustomerCare.DataSource = _dtGetCustomerCare;
                    StoreCustomerCare.DataBind();
                }
                else
                {
                    grCustomerCare.GetStore().RemoveAll();
                }
            }
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private bool CheckInsert()
        {
            bool Check = false;
            var _clsGetCustomerCare = new BllCustomerCare();
            var _dtGetFormCheck = _clsGetCustomerCare.GetCustomerCare(string.Empty);
            if (_dtGetFormCheck != null && _dtGetFormCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtGetFormCheck.Rows)
                {
                    if (txtIDCustomerCare.Text != dr["ID"].ToString())
                    {
                        if (txtCustomerCareCode.Text == dr["Code"].ToString())
                        {
                            Check = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Mã biểu mẫu đã tồn tại !");
                        }
                    }
                }
            }
            return Check;
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtCustomerCare
                    {
                        Code = txtCustomerCareCode.Text,
                        ID_Form = int.Parse(txtIDForm.Text),
                        Subjects = txtTitileSubject.Text,
                        Content = txtNote.Text,
                        DateBegin = DateTime.Parse(txtDateBegin.Text),
                        DateEnd = DateTime.Parse(txtDateEnd.Text),
                        IsActive = chkCheck.Checked,
                        Users_Id = int.Parse(Session["Users_Id"].ToString()),
                    };
                    BllCustomerCare.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    GetCustomerCare();
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.ToString());
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtCustomerCare
                    {
                        ID = int.Parse(txtIDCustomerCare.Text),
                        Code = txtCustomerCareCode.Text,
                        ID_Form = int.Parse(txtIDForm.Text),
                        Subjects = txtTitileSubject.Text,
                        Content = txtNote.Text,
                        DateBegin = DateTime.Parse(txtDateBegin.Text),
                        DateEnd = DateTime.Parse(txtDateEnd.Text),
                        IsActive = chkCheck.Checked
                    };
                    BllCustomerCare.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetCustomerCare();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private bool CheckCustomerCareDetail(int Id)
        {
            bool Check = false;
            var _clsGetCustomerCareDetail = new BllCustomerCareDetail();
            var _dtGetFormCheck = _clsGetCustomerCareDetail.GetCustomerCareDetailDetailTemp(1);
            if (_dtGetFormCheck != null && _dtGetFormCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtGetFormCheck.Rows)
                {
                    if (Id == int.Parse(dr["Customer_ID"].ToString()))
                    {
                        Check = true;
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Khách hàng đã tồn tại !");
                    }
                }
            }
            return Check;
        }
        private bool CheckCustomerCareDetailUpdate(int Id)
        {
            bool Check = false;
            var _clsGetCustomerCareDetail = new BllCustomerCareDetail();
            var _dtGetFormCheck =
                _clsGetCustomerCareDetail.GetCustomerCareDetailDetailEdit(int.Parse(txtIDCustomerCare.Text), 0);
            if (_dtGetFormCheck != null && _dtGetFormCheck.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtGetFormCheck.Rows)
                {
                    if (Id == int.Parse(dr["Customer_ID"].ToString()))
                    {
                        Check = true;
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Khách hàng đã tồn tại !");
                    }
                }
            }
            return Check;
        }
        private void InsertCustomerCareDetail()
        {
            try
            {
                var sm = grCustomerCareAdd.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        try
                        {
                            if (CheckCustomerCareDetail(int.Parse(row.RecordID)))
                            {
                                HTML.clsFormat.Alert_ERROR("Lỗi",
                                    "Khách hàng này đã tồn tại !");
                            }
                            else
                            {
                                var obj = new EtCustomerCareDetail
                                {
                                    ID_CustomerCare = null,
                                    Customer_ID = int.Parse(row.RecordID),
                                    Subjects = string.Empty,
                                    Content = string.Empty,
                                    Flag = 1,
                                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                                };
                                BllCustomerCareDetail.Insert(obj);
                                HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                            }
                        }
                        catch (Exception ex)
                        {
                            HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                        }
                    }
                GetCustomerCareTemp();
                WindowCustomerCare.Close();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateCustomerCareDetail()
        {
            try
            {
                var sm = grCustomerCareAdd.GetSelectionModel() as CheckboxSelectionModel;
                if (sm != null)
                    foreach (var row in sm.SelectedRows)
                    {
                        try
                        {
                            if (CheckCustomerCareDetailUpdate(int.Parse(row.RecordID)))
                            {
                                HTML.clsFormat.Alert_ERROR("Lỗi",
                                    "Khách hàng này đã tồn tại !");
                            }
                            else
                            {
                                var obj = new EtCustomerCareDetail
                                {
                                    ID_CustomerCare = int.Parse(txtIDCustomerCare.Text),
                                    Customer_ID = int.Parse(row.RecordID),
                                    Subjects = string.Empty,
                                    Content = string.Empty,
                                    Flag = 0,
                                    Users_Id = int.Parse(Session["Users_Id"].ToString()),
                                };
                                BllCustomerCareDetail.Insert(obj);
                                HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                            }
                        }
                        catch (Exception ex)
                        {
                            HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                        }
                    }
                GetCustomerCareEdit(int.Parse(txtIDCustomerCare.Text));
                WindowCustomerCare.Close();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[AutoCode]
        protected string AutoCode()
        {
            int i;
            var _clsGetCode = new BllCustomerCare();
            var _dtGetCustomerCare = _clsGetCode.GetCustomerCare(string.Empty);
            if (_dtGetCustomerCare != null && _dtGetCustomerCare.Rows.Count > 0)
            {
                int max = int.Parse(_dtGetCustomerCare.Rows[0]["ID"].ToString());
                for (i = 0; i < _dtGetCustomerCare.Rows.Count; i++)
                {
                    if (int.Parse(_dtGetCustomerCare.Rows[i]["ID"].ToString()) > max)
                    {
                        max = int.Parse(_dtGetCustomerCare.Rows[i]["ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "CTMC_00" + i;
        }
        #endregion

        #region[Clear-Text]
        private void ClearText()
        {
            FormPanelCustomerCare.Reset();
            txtDateBegin.Text = DateTime.Now.ToString();
            txtDateEnd.Text = DateTime.Now.ToString();
            var obj = new EtCustomerCareDetail { Users_Id = int.Parse(Session["Users_Id"].ToString()) };
            BllCustomerCareDetail.DeleteTemp(obj);
            txtCustomerCareCode.Text = AutoCode();
            grCustomerCare_Detail.GetStore().RemoveAll();
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.CustomerCare, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelCustomerCare.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnSendMail.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.CustomerCare, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}